using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using OgilvyOne.IDAL;
using OgilvyOne.BLL.SysManage;

namespace OgilvyOne.ProcessFlow
{
    public class ItemController
    {
        // Fields
        private const string URL_DEFAULT = "/systemmanage/itemmanage/item_list.aspx";
        private const string URL_ITEMCREATE = "/systemmanage/itemmanage/item_add.aspx";
        private const string URL_ITEMUPDATE = "/systemmanage/itemmanage/item_modify.aspx";

        // Methods
        public string ConvertItem(int Item_ID)
        {
            Item item = new Item();
            return item.ConvertItem(Item_ID);
        }

        public bool CreateItem(ItemInfo newItemInfo)
        {
            try
            {
                new Item().Insert(newItemInfo);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool CreateRadioTreeView(TreeView tv, int pID)
        {
            try
            {
                Tree tree = new Tree();
                ITree tree2 = OgilvyOne.DALFactory.Item.Create();
                TreeNode tNode = tree.CreateRoot(tv, pID, tree2);
                tree.TreeViewRadioBind(tNode, pID, tree2);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv, int pID)
        {
            try
            {
                Tree tree = new Tree();
                ITree tree2 = OgilvyOne.DALFactory.Item.Create();
                TreeNode tNode = tree.CreateRoot(tv, pID, tree2);
                tree.TreeViewBind(tNode, pID, tree2);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool CreateTreeView(TreeView tv, string addUrl, string uptUrl, string delUrl)
        {
            try
            {
                Tree tree = new Tree();
                TreeNode tNode = tree.CreateRoot(tv, addUrl, uptUrl, delUrl);
                ITree tree2 = OgilvyOne.DALFactory.Item.Create();
                tree.TreeViewBind(tNode, 0, addUrl, uptUrl, delUrl, tree2, "0");
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteItem(int iNodeID)
        {
            new Item().Delete(iNodeID);
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
        }

        public int getItemIDByDesc(string item_value, int parent_item_id)
        {
            Item item = new Item();
            return item.getItemIDByDesc(item_value, parent_item_id);
        }

        public bool InitDropDownList(DropDownList dropdownlist, int parent_item_id, string selected_value)
        {
            try
            {
                Tree tree = new Tree();
                IItem item = OgilvyOne.DALFactory.Item.Create();
                tree.DropDownListBind(dropdownlist, parent_item_id, item, selected_value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void UpdateItem(ItemInfo updatedItemInfo)
        {
            new Item().Update(updatedItemInfo);
            HttpContext.Current.Response.Redirect(URL_DEFAULT, true);
        }

    }
}
